package com.atwjy.auth.controller;


import com.atguigu.model.system.SysRole;
import com.atguigu.vo.system.AssginRoleVo;
import com.atguigu.vo.system.SysRoleQueryVo;
import com.atwjy.auth.service.SysRoleService;
import com.atwjy.common.Result.Result;
import com.atwjy.config.Exception.GuiguException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Api(tags="角色管理接口")
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    //@PreAuthorize("hasAuthority('bnt.sysRole.toAssign')")
    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String, Object> roleMap = sysRoleService.findRoleByAdminId(userId);
        return Result.ok(roleMap);
    }

    //@PreAuthorize("hasAuthority('bnt.sysRole.doAssign')")
    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }

    //@PreAuthorize("hasAuthority('bnt.sysRole.findAll')")
    @ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    public Result findAll(){
        try {
            int a=1/0;
        } catch (Exception e) {
            throw new GuiguException(20001,"执行了自定义异常");
        }
        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }

    //@PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result pageQueryRole(@PathVariable Long page,
                                @PathVariable Long limit,
                                SysRoleQueryVo sysRoleQueryVo){
        Page<SysRole> pageParam=new Page<>(page,limit);
        LambdaQueryWrapper<SysRole> wrapper=new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if(!StringUtils.isEmpty(roleName)){
            wrapper.like(SysRole::getRoleName,roleName);
        }
        IPage<SysRole> page1 = sysRoleService.page(pageParam, wrapper);
        return Result.ok(page1);
    }
   //@PreAuthorize("hasAuthority('bnt.sysRole.save')")
    @ApiOperation("添加角色")
    @PostMapping ("/save")
    public Result save(@RequestBody SysRole role){
        boolean is_Success = sysRoleService.save(role);
        if(is_Success){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }
    //@PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查询")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id){
        SysRole role = sysRoleService.getById(id);
        return Result.ok(role);
    }
    //@PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("修改角色")
    @PutMapping ("/update")
    public Result update(@RequestBody SysRole role){
        boolean is_Success = sysRoleService.updateById(role);
        if(is_Success){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }
    //@PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("根据id删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id){
        boolean is_Success = sysRoleService.removeById(id);
        if(is_Success){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }
    //@PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        boolean is_Success = sysRoleService.removeByIds(idList);
        if(is_Success){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }
}
